<?php
declare (strict_types=1);

namespace app\common\model\category\admin;

use app\admin\logic\system\Config as SystemConfig;
use app\common\event\subscribes\category\admin\Category as CategorySubscribe;
use app\common\model\BaseModel;
use think\facade\Event;
use think\facade\Request;
use think\model\concern\SoftDelete;

/**
 * 后台管理
 */
class Category extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'category';

    protected $categoryType = false;

    // 是否自动检测分类类型,当开启全局分类时,获取category_type = 0的数据
    protected $autoCheckUsePublic = false;

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['mhm'];

    /**
     * 定义查询分类类型范围条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-10
     *
     * @param    [type]                         $query [description]
     */
    public function getCategoryType($type = 0)
    {
        // 单独处理课程分类
        if ($this->autoCheckUsePublic === true) {
            $config = SystemConfig::get('system.category', ['use_public' => 0], request()->mhm_id);
            if ($config['use_public'] == 1) {
                // 开启了全局分类, 所有分类都调用课程分类
                $type = 0;
            }
        } else {
            if ($this->categoryType === false) {
                $type = Request::route('category_type/d', 3);
            }
        }
        return $type;
    }

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     *
     * @param    [type]                         $query [description]
     */
    public function scopeMhm($query)
    {
        // 机构
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 设置自动检测是否开启了全局分类
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-19
     *
     * @param boolean $auto [description]
     */
    public function autoCheckUsePublic(bool $auto = true): Category
    {
        $this->autoCheckUsePublic = $auto;

        return $this;
    }

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     */
    public static function onBeforeInsert($model)
    {
        // 分类类型
        $model->category_type = Request::route('category_type/d', 0);
        // 机构
        $model->mhm_id = $model->getAttr('mhm_id') ?: request()->mhm_id;
    }

    /**
     * 成功创建之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterInsert($category)
    {
        Event::subscribe(CategorySubscribe::class);
        Event::trigger('CategoryCreated', $category);
    }

    /**
     * 成功修改之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterUpdate($category)
    {
        Event::subscribe(CategorySubscribe::class);
        Event::trigger('CategoryUpdated', $category);
    }

    /**
     * 成功删除之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterDelete($category)
    {
        Event::subscribe(CategorySubscribe::class);
        Event::trigger('CategoryDeleted', $category);
    }
}
